#include <iostream>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>


void word(int x)
{
    const char* message="hello world";
    write(x,message,strlen(message));
    sleep(10);
    std::cout<<"child quit"<<std::endl;
}

int main()
{
    int a[2]={0};
    int n = pipe(a);
    if(n==-1) return 1;
    pid_t id = fork();
    if(id==0)//写
    {
        close(a[0]);
        std::cout<<"I am a Child"<<std::endl;
        word(a[1]);
        close(a[1]);
        exit(0);
    }
    close(a[1]);
    char s[1000]={0};
    n = read(a[0],s,999);
    s[n]='\0';
    std::cout<<"This is Father : "<<s<<std::endl;
    int rid = waitpid(id,nullptr,0);
    std::cout<<"Father quit"<<std::endl;
    return 0;
}
